package com.taojie.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taojie.graduation.domain.Car;
import com.taojie.graduation.domain.Ordert;
import com.taojie.graduation.mapper.OrdertMapper;
import com.taojie.graduation.service.api.CarService;
import com.taojie.graduation.service.api.LogisticsService;
import com.taojie.graduation.service.api.OrdertService;
import com.taojie.graduation.utils.PageResult;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrdertServiceImpl implements OrdertService {
    @Resource
    OrdertMapper ordertMapper;

    @Resource
    CarService carService;

    @Resource
    LogisticsService logisticsService;
    /**
     * TODO 分页查
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result findOrdertPage(int page, int size) {
        Page<Ordert> userPage = new Page<>(page, size);
        Page<Ordert> ordertPage1 = ordertMapper.selectPage(userPage, null);
        // 根据id排序
        List<Ordert> collect = ordertPage1.getRecords().stream().sorted(Comparator.comparing(Ordert::getId)).collect(Collectors.toList());
        if (!(ordertPage1.getRecords().size() > 0)) {
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS, new PageResult<Ordert>(ordertPage1.getTotal(), collect));
    }

    /**
     * TODO 根据id查
     * @param id
     * @return
     */
    @Override
    public Result findOrdertById(Integer id) {
        if (id < 0) { return  Result.INPUT_ERROR(); }
        Ordert ordert = ordertMapper.selectById(id);
        if (ObjectUtils.isEmpty(ordert)){
            return Result.FAIL();
        }
        return new Result(ResultCode.SUCCESS, ordert);
    }

    /**
     * TODO 更新
     * @param ordert
     * @return
     */
    @Override
    public Result updateById(Ordert ordert) {
        if (ordert.getId() < 0) {
            return Result.INPUT_ERROR();
        }
        int i = ordertMapper.updateById(ordert);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        if (id < 0) {
            return Result.INPUT_ERROR();
        }
        int i = ordertMapper.deleteById(id);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 新增订单
     * @param ordert 订单内容
     * @param userid 用户id
     * @param productid 商品id
     * @param storeid 商店id
     * @return
     */
    @Override
    public Result add(Ordert ordert, Integer userid, Integer productid, Integer storeid) {
        Car car = carService.findByUserId(userid);
        if (ObjectUtils.isEmpty(car)) {
            return new Result(ResultCode.NOT_FOUND);
        }
        ordert.setCarId(car.getId());
        ordert.setCreateTime(new Date());
        ordert.setProductId(productid);
        ordert.setSeri(IdWorker.getIdStr());
        int i = ordertMapper.insert(ordert);
        QueryWrapper<Ordert> wrapper = new QueryWrapper<>();
        wrapper.eq("seri", ordert.getSeri());
        Ordert ot = ordertMapper.selectOne(wrapper);
        int i1 = logisticsService.addLogistics(car.getAddress(), storeid, ot.getId());
        if (i <= 0 || i1 < 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }
}
